package icu.ikuns;

import java.sql.*;

/**
 * @Author： Kimariyb
 * @Date：2022/05/14/19:25
 * @Description：
 */
public class DBConnection {
    private static String url = "jdbc:mysql://localhost:3306/kimari?useUnicode=true&" +
            "characterEncoding=utf-8&serverTimezone=UTC";
    private static String username = "root";
    private static String password = "123456";
    private static String drivername = "com.mysql.cj.jdbc.Driver";
    static {
        try {
            Class.forName(drivername);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() {
        try {
            Connection connection = DriverManager.getConnection(url, username, password);
            return connection;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static void free(Connection conn, Statement stmt, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
